package hh.util.bean.copy;

import hh.util.bean.ClassInfo;
import hh.util.bean.ClassUtil;
import hh.util.bean.ReflectFieldInfo;

/**
 * @author huanghuai
 * @date 2020/10/26
 */
public abstract class GetSetCopier extends AbstractCopier {

    private static final long serialVersionUID = -3441487741431485807L;

    @Override
    protected <Dest, Source> Dest doCopy(Source sourceObj, Class<Dest> destType) throws Exception {
        Dest destObj = (Dest) ClassUtil.newInstance(destType);
        ClassInfo sourceInfo = getClassInfo(sourceObj.getClass());
        ClassInfo destInfo = getClassInfo(destType);

        for (ReflectFieldInfo destFieldInfo : destInfo.getFieldInfos()) {
            ReflectFieldInfo sourceFieldInfo = sourceInfo.getFieldInfo(destFieldInfo.getFieldName());
            doReflectFieldCopy(sourceFieldInfo, destFieldInfo, sourceObj, destObj);
        }
        return destObj;
    }

    protected abstract <Source, Dest> void doReflectFieldCopy
            (ReflectFieldInfo sourceFieldInfo, ReflectFieldInfo destFieldInfo, Source source, Dest destObj) throws Exception;


}
